// pages/delivery-order/index.js
import { toPrevPage } from "../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    styleSize: { minHeight: 60, maxHeight: 60 },
    images: [],
    id: "",
    data: {},
    workerName: "", // 工人姓名
    workerPhone: "", // 工人电话
    workerNameIsError: "",
    workerPhoneIsError: "",
  },

  handleWorkerNameChange(event) {
    const value = event.detail;

    this.setData({
      workerNameIsError: value ? "" : "请输入工人姓名",
    });
  },

  handleWorkerPhoneChange(event) {
    const value = event.detail;

    this.setData({
      workerPhoneIsError: value ? "" : "请输入工人电话",
    });
  },

  handleSubmit() {
    const { workerName, workerPhone, id } = this.data;

    if (!workerName) {
      this.setData({
        workerNameIsError: "请输入工人姓名",
      });

      return;
    }

    if (!workerPhone) {
      this.setData({
        workerPhoneIsError: "请输入工人电话",
      });

      return;
    }

    const params = {
      id,
      repair_name: workerName,
      repair_phone: workerPhone,
    };

    wx.ajax.post("wxazApplyHandle.action", params, true).then((res) => {
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });

        wx.navigateBack({
          delta: 1,
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });
      }
    });
  },

  handleSure() {
    const { id } = this.data;

    const params = { id };

    wx.ajax.post("wxazApplyHandleFinish.action", params, true).then((res) => {
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });

        wx.navigateBack({
          delta: 1,
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });
      }
    });
  },

  getData() {
    const { id } = this.data;

    const param = { id };

    wx.ajax.post("findWxazApplyById.action", param, true).then((res) => {
      // console.log(res);
      if (res.success) {
        this.setData({
          data: res.result,
          images: res.result.annex.split(","),
          workerName: res.result.repair_name,
          workerPhone: res.result.repair_phone,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });

    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
